package com.situ.company.web220111.department.service.impl;

import com.situ.company.web220111.department.dao.IDepartmentDao;
import com.situ.company.web220111.department.dao.impl.Department2DaoImpl;
import com.situ.company.web220111.department.model.DepartmentModel;
import com.situ.company.web220111.department.service.IDepartmentService;
import com.situ.company.web220111.employee.dao.IEmployeeDao;
import com.situ.company.web220111.employee.dao.impl.EmployeeDaoImpl;
import com.situ.company.web220111.employee.model.EmployeeModel;

import java.util.List;

public class DepartmentServiceImpl implements IDepartmentService {

    private IDepartmentDao dao = new Department2DaoImpl();
    private IEmployeeDao employeeDao = new EmployeeDaoImpl();

    @Override
    public String insert(DepartmentModel model) {
        if (selectModel(model) == null) {
            String res = dao.insert(model);
            return res == null ? null : res;
        }
        return "repeat";
    }


    @Override
    public String delete(DepartmentModel model) {
        // 判断如果部门下有员工就不能删除
        String codeDept = model.getCode();
        EmployeeModel employeeModel = new EmployeeModel();
        employeeModel.setCodeDept(codeDept);
        Integer count = employeeDao.selectCount(employeeModel);
        if(count > 0)
            return "E";
        String res = dao.delete(model);
        return res == null ? null : res.toString();
    }

    @Override
    public String updateAll(DepartmentModel model) {
        return dao.updateAll(model);
    }

    @Override
    public String updateActive(DepartmentModel model) {
        return dao.updateActive(model);
    }

    @Override
    public List<DepartmentModel> selectList(DepartmentModel model) {
        String code = model.getCode();
        model.setCode(code == null ? "%" : "%" + code + "%");
        String name = model.getName();
        model.setName(name == null ? "%" : "%" + name + "%");
        return dao.selectList(model);
    }

    @Override
    public DepartmentModel selectModel(DepartmentModel model) {
        return dao.selectModel(new DepartmentModel(model.getCode()));
    }

    @Override
    public Integer selectCount(DepartmentModel model) {
        DepartmentModel model1 = new DepartmentModel();
        String code = model.getCode();
        model.setCode(code == null ? "%" : "%" + code + "%");
        String name = model.getName();
        model.setName(name == null ? "%" : "%" + name + "%");
        return dao.selectCount(model1);
    }
}
